package com.sky.service.impl;

import com.sky.entity.Orders;
import com.sky.mapper.DishMapper;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.WorkspaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;

/**
 * packageName com.sky.service.impl
 *
 * @author fenglai
 * @version JDK 11
 * @className WorkspaceServiceImpl
 * @date 2023/12/8
 * @description TODO
 */
@Service
public class WorkspaceServiceImpl implements WorkspaceService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private DishMapper dishMapper;


    public List<LocalDate> getDateList(LocalDate begin, LocalDate end) {
        List<LocalDate> Date = new ArrayList<>();
        Date.add(begin);
        // 这里如果不先添加一个进去 会少一次 因为需要包尾
        while (!begin.equals(end)) {
            begin = begin.plusDays(1);
            Date.add(begin);
        }
        return Date;
    }

    @Override
    public BusinessDataVO getBusinessData() {
        // 获得当天的最初始时间
        LocalDateTime begin = LocalDateTime.of(LocalDate.now(), LocalTime.MIN);
        // 获得当天的最后时间
        LocalDateTime end = LocalDateTime.of(LocalDate.now(), LocalTime.MAX);
        // 统计新添的用户量
        Integer newUsers = userMapper.countUserStatistics(begin, end);
        // 获得完成订单的数量
        Integer validOrderCount = orderMapper.countOrders(Orders.COMPLETED, begin, end);
        // 获得总订单数
        Integer allOrdersNumber = orderMapper.countOrders(null, begin, end);
        Double orderCompletionRate = 0.0;
        if (validOrderCount != 0) {
            // 通过公式获得订单的成功率
            orderCompletionRate = 1.0 * validOrderCount / allOrdersNumber;
        }
        // 因为有效订单可能为0 所以 会导致成功率为null  这里需要用三元运算符赋值为0
        // 通过之前在订单管理写好的方法传入日期 获得日期范围内的营业额 当然这里是一天所以直接传
        Integer turnover = orderMapper.countOrdersAmount(Orders.COMPLETED, begin, end);
        // 因为sum如果没有得到结果
        turnover = turnover == null ? 0 : turnover;
        // 接下来是平均客单价 也就是营业额除有效订单数 算出来的就是客人平均消费
        Double unitPrice = 0.0;
        if (validOrderCount != 0) {
            // 因为不一定每天都有完成的订单 需要做非空判断
            unitPrice = 1.0 * turnover / validOrderCount;
        }


        // 需要的数据都已经完善 接下来直接构建vo对象传值返回
        return BusinessDataVO.builder().
                newUsers(newUsers).
                orderCompletionRate(orderCompletionRate).
                turnover(Double.valueOf(turnover)).
                unitPrice(unitPrice).
                validOrderCount(validOrderCount).
                build();

    }

    @Override
    public SetmealOverViewVO overviewSetmeals() {

        return setmealMapper.countSetmealStatus();

    }

    @Override
    public DishOverViewVO overviewDishes() {
        return dishMapper.countDishesStatus();
    }

    @Override
    public OrderOverViewVO overviewOrders() {
        return orderMapper.countAllOrdersStatus();
    }
}